//
//  ZZSettingVC.m
//  新项目
//
//  Created by liGordon on 16/12/21.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "ZZSettingVC.h"
#import "SZKCleanCache.h"
#import "YGKJLoginVC.h"
#import "ZZChangePasswordViewController.h"
#import "ZZAdviceVC.h"
@interface ZZSettingVC ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *updateLabel;
@property (weak, nonatomic) IBOutlet UILabel *cacheLabel;
@property (weak, nonatomic) IBOutlet UIButton *exitBtn;


@end

@implementation ZZSettingVC

// 这里需要在注册之后再写更新版本的逻辑

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"更多设置";
    self.exitBtn.layer.cornerRadius = 10;
    
    self.cacheLabel.text = [NSString stringWithFormat:@"缓存是%.2fM",[SZKCleanCache folderSizeAtPath]/2
                        ];
}
- (IBAction)adviceBtnClick:(UIButton *)sender {
    ZZAdviceVC *vc = [[ZZAdviceVC alloc]init];
    PushVC(vc);
}
- (IBAction)changePWClick:(UIButton *)sender {
    ZZChangePasswordViewController *vc = [[ZZChangePasswordViewController alloc]init];
    PushVC(vc);
}


- (IBAction)updateBtnClick:(UIButton *)sender {
    
}
- (IBAction)clearCacheBtnClick:(UIButton *)sender {
    //清楚缓存
    NSString *title = NSLocalizedString(@"是否确定清楚缓存", nil);
    //    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);
    NSString *confirmButtonTitle = NSLocalizedString(@"确定", nil);
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:confirmButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [SZKCleanCache cleanCache:^{
            DLog(@"清除成功");
            _cacheLabel.text = @"缓存是0.00M";
        }];
    }];
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (IBAction)exitLoginBtnClick:(UIButton *)sender {
    NSString *title = NSLocalizedString(@"是否确定退出登录", nil);
    //    NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"确定", nil);
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        

        YGKJLoginVC *vc = [[YGKJLoginVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
   
        
        
    }];
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [self presentViewController:alertController animated:YES completion:nil];

}


// 更新方法实现
-(void)VersionUpdate
{
    // 获取发布版本的Version
    NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@""] encoding:NSUTF8StringEncoding error:nil];
    if (string!=nil && [string length]>0 && [string rangeOfString:@"version"].length == 7) {
        
    }
}

#pragma mark -- 比较当前版本与新上线版本做比较
-(void)checkAppUpdate:(NSString *)appInfo
{
    //获取当前版本
    NSString *version = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleVersion"];
    NSString *appInfo1 = [appInfo substringFromIndex:[appInfo rangeOfString:@"\"verision\":"].location+10];
    appInfo1 = [[appInfo1 substringFromIndex:[appInfo1 rangeOfString:@","].location]stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    //判断,如果当前版本与发布版本不相同则进入更新.如果相等 那么当前就是最高版本
    if (![appInfo1 isEqualToString:version]) {
        NSLog(@"新版本:%@,当前版本%@",appInfo1,version);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:[NSString stringWithFormat:@"新版本 %@已发布!",appInfo1] delegate:self.class cancelButtonTitle:@"知道了" otherButtonTitles: nil];
        alert.delegate = self;
        [alert addButtonWithTitle:@"前往更新"];
        [alert show];
        alert.tag = 20;
    }else{
      [[[UIAlertView alloc]initWithTitle:nil message:@"已是最高版本" delegate:self.class cancelButtonTitle:@"知道了" otherButtonTitles:nil]show];
    }
                         
}


-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1 & alertView.tag == 20) {
        NSString *url = @"";
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
        
        
    }
}
@end
